package com.tjsinfo.tjsoa.meetingroom.dao;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;
import org.hibernate.Criteria;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.tjsinfo.tjsoa.meetingroom.vo.IMeetingRoomEmploy;
import com.tjsinfo.tjsoa.meetingroom.vo.MeetingRoomEmploy;
import com.tjsinfo.tjsoa.meetingroom.vo.MeetingRoomFileUpload;

/**
 * A data access object (DAO) providing persistence and search support for
 * MeetingRoomEmploy entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.tjsinfo.tjsoa.meetingroom.employ.MeetingRoomEmploy
 * @author MyEclipse Persistence Tools
 */

public class MeetingRoomEmployDAO extends HibernateDaoSupport implements IMeetingRoomEmployDAO {
	private static final Log log = LogFactory
			.getLog(MeetingRoomEmployDAO.class);
	// property constants
	public static final String MEETING_ID = "meetingId";
	public static final String PEOPLE_ID = "peopleId";
	public static final String MEETING_TIME = "meetingTime";
	public static final String MEETING_CONTENT = "meetingContent";
	public static final String REMARKS = "remarks";
	public static final String MINUTES = "minutes";
	public static final String STATE = "state";
	public static final String START_TIME = "startTime";
	public static final String END_TIME = "endTime";
	public static final String END_YEAR = "endYear";
	public static final String END_MONTH = "endMonth";
	public static final String MEETING_NAME = "meetingName";
	public static final String EXAMINE_USER_ID = "examineUserId";
	public static final String EMPOLY_TIME = "Empoly_time";
	public static final String RE_USE = "Re_use";
	public static final String APPLICATION_USER_ID = "applicationUserId";

	protected void initDao() {
		// do nothing
	}

	public void save(IMeetingRoomEmploy transientInstance) {
		log.debug("saving MeetingRoomEmploy instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void save(Object transientInstance) {
		log.debug("saving MeetingRoomFileUpload instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Object persistentInstance) {
		log.debug("deleting MeetingRoomEmploy instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public void delete(IMeetingRoomEmploy persistentInstance) {
		log.debug("deleting MeetingRoomEmploy instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public IMeetingRoomEmploy findById(String id) {
		log.debug("getting MeetingRoomEmploy instance with id: " + id);
		try {
			IMeetingRoomEmploy instance = (IMeetingRoomEmploy) getHibernateTemplate()
					.get(
							"com.tjsinfo.tjsoa.meetingroom.vo.MeetingRoomEmploy",
							id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(IMeetingRoomEmploy instance) {
		log.debug("finding MeetingRoomEmploy instance by example");
		
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding MeetingRoomEmploy instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from MeetingRoomEmploy as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
    
	public List findByTwoProperty(String propertyName, Object value,String propertyName2, Object value2) {
		log.debug("finding MeetingRoomEmploy instance with property: "
				+ propertyName + ", value: " + value+"property:"+propertyName2+", value:"+value2);
		try {
			List list;
			Session session = getSession();
			Criteria criteria = session.createCriteria(MeetingRoomEmploy.class);
			criteria.add(Expression.eq(propertyName, value));
			criteria.add(Expression.eq(propertyName2, value2));
			list=criteria.list();
			session.close();
			return list;
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
	public List findByIdAndDate(String propertyName, Object value,String propertyName2, Object value2) {
		log.debug("finding MeetingRoomEmploy instance with property: "
				+ propertyName + ", value: " + value+"property:"+propertyName2+", value:"+value2);

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		List list = new ArrayList();
		try {
			Session session = getSession();
			Criteria criteria = session.createCriteria(MeetingRoomEmploy.class);
			criteria.add(Expression.eq(propertyName, value));
			try {
				java.sql.Date qian = new java.sql.Date(sdf.parse(value2.toString()+" 00:00:00").getTime()); 
				java.sql.Date hou = new java.sql.Date(sdf.parse(value2.toString()+" 23:59:59").getTime());
				criteria.add(Expression.between(propertyName2, qian, hou));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			list=criteria.list();
			session.close();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
		return list;
	}
	
	public List findByThrProperty(String propertyName, Object value,String propertyName2, Object value2,String propertyName3, Object value3) {
		log.debug("finding MeetingRoomEmploy instance with property: "
				+ propertyName + ", value: " + value+"property:"+propertyName2+", value:"+value2);
		try {
			List list;
			Session session = getSession();
			Criteria criteria = session.createCriteria(MeetingRoomEmploy.class);
			criteria.add(Expression.eq(propertyName, value));
			criteria.add(Expression.eq(propertyName2, value2));
			criteria.add(Expression.eq(propertyName3, value3));
			list=criteria.list();
			session.close();
			return list;
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
	public List findByLikeThrProperty(String propertyName, Object value,String propertyName2, Object value2,String propertyName3, Object value3) {
		log.debug("finding MeetingRoomEmploy instance with property: "
				+ propertyName + ", value: " + value+"property:"+propertyName2+", value:"+value2);
		try {
			List list;
			Session session = getSession();
			Criteria criteria = session.createCriteria(MeetingRoomEmploy.class);
			criteria.add(Expression.eq(propertyName, value));
			criteria.add(Expression.eq(propertyName2, value2));
			criteria.add(Expression.like(propertyName3, value3));
			list=criteria.list();
			session.close();
			return list;
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
	public List findByMeetingRoomId(String id){
		try {
			List list;
			Session session = getSession();
			Criteria criteria = session.createCriteria(MeetingRoomEmploy.class);
			criteria.add(Expression.eq("meetingRoomMain.id", id));
			list=criteria.list();
			session.close();
			return list;
			
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
	
	public List findByMeetingId(Object meetingId) {
		return findByProperty(MEETING_ID, meetingId);
	}
   
	public List findByPeopleId(Object peopleId) {
		return findByProperty(PEOPLE_ID, peopleId);
	}

	public List findByMeetingTime(Object meetingTime) {
		return findByProperty(MEETING_TIME, meetingTime);
	}

	public List findByMeetingContent(Object meetingContent) {
		return findByProperty(MEETING_CONTENT, meetingContent);
	}

	public List findByRemarks(Object remarks) {
		return findByProperty(REMARKS, remarks);
	}

	public List findByMinutes(Object minutes) {
		return findByProperty(MINUTES, minutes);
	}

	public List findByState(Object state) {
		return findByProperty(STATE, state);
	}

	public List findByStartTime(Object startTime) {
		return findByProperty(START_TIME, startTime);
	}

	public List findByEndTime(Object endTime) {
		return findByProperty(END_TIME, endTime);
	}

	public List findByEndYear(Object endYear) {
		return findByProperty(END_YEAR, endYear);
	}

	public List findByEndMonth(Object endMonth) {
		return findByProperty(END_MONTH, endMonth);
	}

	public List findByMeetingName(Object meetingName) {
		return findByProperty(MEETING_NAME, meetingName);
	}

	public List findByExamineUserId(Object examineUserId) {
		return findByProperty(EXAMINE_USER_ID, examineUserId);
	}
    
	public List findByEmpolyTime(Object Empoly_time) {
		return findByProperty(EMPOLY_TIME, Empoly_time);
	}
	
	public List findAll() {
		log.debug("finding all MeetingRoomEmploy instances");
		try {
			String queryString = "from MeetingRoomEmploy";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public IMeetingRoomEmploy merge(IMeetingRoomEmploy detachedInstance) {
		log.debug("merging MeetingRoomEmploy instance");
		try {
			IMeetingRoomEmploy result = (IMeetingRoomEmploy) getHibernateTemplate()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(IMeetingRoomEmploy instance) {
		log.debug("attaching dirty MeetingRoomEmploy instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(IMeetingRoomEmploy instance) {
		log.debug("attaching clean MeetingRoomEmploy instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
    
	/**
	 * 更新
	 */
	public void update(Object object) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().update(object);
	}
	
	public static MeetingRoomEmployDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (MeetingRoomEmployDAO) ctx.getBean("MeetingRoomEmployDAO");
	}
}